package cn.changsheng.csfenjie.command;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import cn.changsheng.csfenjie.main;
import cn.changsheng.csfenjie.api.FenJieApi;
import cn.changsheng.csfenjie.config.itemData;
import cn.changsheng.csfenjie.utils.CommonlyUtil;
import cn.changsheng.csfenjie.utils.ItemSerialize;

public class ExaCommand implements CommandExecutor, TabExecutor {

	@SuppressWarnings("deprecation")
	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

		switch (command.getName()) {
		case "fj":
			if (args.length == 0) {
				if (sender.isOp()) {
					sender.sendMessage("§7/fj start §6分解手中物品");
					sender.sendMessage("§7/fj startall §6分解所有手中物品");
					sender.sendMessage("§7/fj set [id] §6设置手中物品为分解物(数量也将一起保存)");
					sender.sendMessage("§7/fj reload §6重载插件配置文件");
				} else {
					sender.sendMessage("§7/fj start §6分解手中物品");
					sender.sendMessage("§7/fj startall §6分解所有手中物品");
					sender.sendMessage("§7/fj open §6打开分解Gui");
				}
			}
			if (sender.isOp()) {
				if (args.length != 0) {
					switch (args[0]) {
					case "set":
						if (args.length == 1) {
							sender.sendMessage("§7/fj set [id] §6设置手中物品为分解物(数量也将一起保存)");
						}
						set(sender, command, label, args);
						break;
					case "reload":
						reload(sender, command, label, args);
						break;
					case "start":
						if (sender instanceof Player) {
							fj(sender, command, label, args);
							return false;
						}
					case "startall":
						if (sender instanceof Player) {
							int amount = ((Player) sender).getItemInHand().getAmount();
							if (((Player) sender).getItemInHand() != null) {
								for (int i = 0; i <= amount; i++) {
									fj(sender, command, label, args);
								}
							}
						}
						return false;
					default:
						if (sender.isOp()) {
							sender.sendMessage("§7/fj start §6分解手中物品");
							sender.sendMessage("§7/fj startall §6分解所有手中物品");
							sender.sendMessage("§7/fj set [id] §6设置手中物品为分解物(数量也将一起保存)");
							sender.sendMessage("§7/fj reload §6重载插件配置文件");
						} else {
							sender.sendMessage("§7/fj start §6分解手中物品");
							sender.sendMessage("§7/fj startall §6分解所有手中物品");
							sender.sendMessage("§7/fj open §6打开分解Gui");
						}
						break;
					}
				}
				break;
			}
		}
		return true;
	}

	private void fj(CommandSender sender, Command command, String label, String[] args) {
		Player player = Bukkit.getPlayer(sender.getName());
		ArrayList<ItemStack> startFenJie = FenJieApi.startFenJie(((Player) sender).getItemInHand(), player);
		if (startFenJie != null) {
			if (startFenJie.size() != 0) {
				// 扣除玩家手中物品
				if (player.getItemInHand().getAmount() != 1) {
					player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1);
				}
				if (player.getItemInHand().getAmount() == 1) {
					player.setItemInHand(null);
				}
				// 给玩家增加物品
				for (ItemStack itemStack : startFenJie) {
					player.getInventory().addItem(itemStack);
				}
				sender.sendMessage("§7[分解] §a分解成功请查看背包");
			}

			if (startFenJie.size() == 0) {
				// 扣除玩家手中物品
				if (player.getItemInHand().getAmount() != 1) {
					player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1);
				}
				if (player.getItemInHand().getAmount() == 1) {
					player.setItemInHand(null);
				}
				sender.sendMessage("§7[分解] §c分解失败");
			}
		}
	}

	private void reload(CommandSender sender, Command command, String label, String[] args) {
		main.getInstance().reloadConfig();
		sender.sendMessage("§7[分解] §a重载成功");
	}

	@SuppressWarnings("deprecation")
	private void set(CommandSender sender, Command command, String label, String[] args) {
		if (args.length >= 2) {
			Player player = Bukkit.getPlayer(sender.getName());
			boolean set = itemData.set(args[1], new ItemSerialize().getItemString(player.getItemInHand()));
			if (set) {
				sender.sendMessage("§7[分解] §a保存成功");
			} else {
				sender.sendMessage("§7[分解] §a保存失败");
			}
		}

	}

	@Override
	public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
		return null;
	}

}
